<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>角色权限</title>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/statics/zTree_v3/css/zTreeStyle/zTreeStyle.css">
    <script src="<%=request.getContextPath()%>/statics/zTree_v3/js/jquery-1.4.4.min.js"></script>
    <script src="<%=request.getContextPath()%>/statics/zTree_v3/js/jquery.ztree.all.min.js"></script>
    <script>
        $(function(){
            var setting = {
                view:{
                    showLine:true
                },
                data:{
                    simpleData: {
                        enable:true,
                        idKey: "id",
                        pIdKey: "pId",
                        rootPId: null
                    }
                },
                check:{
                    enable:true
                }
            };
            $.ajax({
                url:"<%=request.getContextPath()%>/leftMenu.html",
                type:"GET",
                dataType:"json",
                success:function (data) {
                    ztreeObj=$.fn.zTree.init($("#treeDemo"),setting,data);
                    ztreeObj.expandAll(true);
                    getCheck(ztreeObj);
                    $("#btn").click(function(){
                        submitCheck(ztreeObj);
                    });
                }
            });

        });
        function getCheck(ztreeObj) {
            $.ajax({
                url:"<%=request.getContextPath()%>/role/getCheck.html?roleId=${roleId}",
                type:"GET",
                dataType:"json",
                success:function (data) {
                    for (var i = 0;i<data.length;i++){
                        ztreeObj.checkNode(ztreeObj.getNodeByParam("id",data[i]),true);
                    }
                }
            })
        }
        function submitCheck(ztreeObj){
            var nodes = ztreeObj.getCheckedNodes(true);
            var arr = new Array();
            for (var i = 0;i<nodes.length;i++){
                arr.push(nodes[i].id);
            }
            var menuIds = arr.join(",");
            $.ajax({
                url:"<%=request.getContextPath()%>/role/assignPerm.html?roleId=${roleId}"+"&menuIds="+menuIds,
                type:"post",
                success:function (data) {
                    if(data){
                        alert("分配成功");
                    }else {
                        alert("分配失败");
                    }
                }
            })
        }

    </script>
</head>
<body>
    <div id="leftsdd">
        <ul id="treeDemo" class="ztree"></ul>
    </div>
    <input type="button" value="分配权限" id="btn"/>
</body>
</html>
